package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AuthSupport;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangYuJie
 * projectName:shop_manage
 * time:2022/5/17 20:24
 * description:
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin/storage")
public class DtsAdminStorageController {
    private static final Logger logger = LoggerFactory.getLogger(DtsAdminStorageController.class);

    @Autowired
    DtsStorageService dtsStorageServiceImpl;

    /**
     * 对象存储列表查询
     */
    @RequiresPermissions("admin:storage:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "查询")
    @GetMapping("/list")
    public Object list(String key, String name, @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->对象存储->查询,请求参数,name:{},key:{},page:{}", name, key, page);

        IPage<DtsStorage> iPage = dtsStorageServiceImpl.querySelective(key, name, page, limit, sort, order);
        long total = iPage.getTotal();
        List<DtsStorage> list = iPage.getRecords();
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", list);

        logger.info("【请求结束】系统管理->对象存储->查询:响应结果:{}", JSONObject.toJSONString(data));
        return ResponseUtil.ok(data);
    }

    /**
     * 上传保存文件
     * @param file
     * @return
     * @throws
     */
    @RequiresPermissions("admin:storage:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "上传")
    @PostMapping("/create")
    public Object create(@RequestParam("file") MultipartFile file) throws Exception {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->对象存储->上传,请求参数,file:{}", file.getOriginalFilename());

        //文件名
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isEmpty(originalFilename)) {
            throw new RuntimeException("上传的文件为空, 不允许上传!");
        }
        //文件扩展名
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        //上传保存
        DtsStorage storage = dtsStorageServiceImpl.add(originalFilename, (int) file.getSize(), ext, file.getBytes());

        logger.info("【请求结束】系统管理->对象存储->查询:响应结果:{}", JSONObject.toJSONString(storage));
        return ResponseUtil.ok(storage);
    }

    /**
     * 更新文件
     * @param dtsStorage
     * @return
     */
    @RequiresPermissions("admin:storage:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsStorage dtsStorage) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->对象存储->编辑,请求参数:{}", JSONObject.toJSONString(dtsStorage));

        if (dtsStorageServiceImpl.update(dtsStorage) == 0) {
            logger.error("系统管理->对象存储->编辑 错误:{}", "更新数据失败!");
            return ResponseUtil.updatedDataFailed();
        }

        logger.info("【请求结束】系统管理->对象存储->编辑:响应结果:{}", JSONObject.toJSONString(dtsStorage));
        return ResponseUtil.ok(dtsStorage);
    }

    /**
     * 删除文件
     * @param DtsStorage
     * @return
     */
    @RequiresPermissions("admin:storage:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage DtsStorage) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->对象存储->删除,请求参数:{}", JSONObject.toJSONString(DtsStorage));

        String key = DtsStorage.getKey();
        if (StringUtils.isEmpty(key)) {
            return ResponseUtil.badArgument();
        }
        dtsStorageServiceImpl.deleteByKey(key);

        logger.info("【请求结束】系统管理->对象存储->删除:响应结果:{}", "成功!");
        return ResponseUtil.ok();
    }
}
